home *** CD-ROM | disk | FTP | other *** search
- unit Lpunit;
- {
- PC Plus sample Delphi application.
-
- A simple drag-and-drop-enabled application 'launch pad'.
- * This version can load document files with the icon of their
- associated applications (e.g. if Word is installed, a .DOC file will
- display the Word icon.
- * A menu has been added.
- * It lets you save and load your icon bar.
- * It loads a previously saved icon bar automically when it is run.
- * A menu option lets you clear the buttons from the bar.
- * A horizontal scroll bar appears when buttons extend beyond the form edge.
- * Various error messages are displayed when files can't be loaded or run.
-
- Usage: Select 1 or more files in the Windows Explorer and drag/drop
- them onto the launch pad. Those that contain an icon will be shown as
- an iconic button, those without an icon will be shown as a plain labelled
- button. Once installed, a button can be clicked to launch the
- associated application.
-
- The main additions to this version are indicated by comments starting
- with the characters:
- {!!
-
- Limitations: Could do with more error checking/recovery.
-
- Compatibility: Delphi 2.0 and above
-
- Author: Huw Collingbourne
- }
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs,
- ShellAPI, StdCtrls, Buttons, ExtCtrls, Menus; {!! must use the ShellAPI unit }
- {!! BitBtn needs the Buttons unit }
-
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- FileMenu: TMenuItem;
- SaveMenuItem: TMenuItem;
- ReloadMenuItem: TMenuItem;
- ClearAllMenuItem: TMenuItem;
- N1: TMenuItem;
- ExitMenuItem: TMenuItem;
- procedure FormCreate(Sender: TObject);
- procedure SaveMenuItemClick(Sender: TObject);
- procedure ReloadMenuItemClick(Sender: TObject);
- procedure ClearAllMenuItemClick(Sender: TObject);
- procedure ExitMenuItemClick(Sender: TObject);
- procedure FormShow(Sender: TObject);
- private
- { Private declarations }
- ButtonList : TStringList; {!! This StringList holds our BitBtns }
- procedure RespondToMessage(var Msg: Tmsg; var Handled: Boolean);
- procedure BtnClick(Sender: TObject);
- procedure AddBtn( fname : string );
- procedure ClearAll;
- procedure AddErrorMsg( msg : string );
- procedure ReloadButtons;
- procedure DealWithScrollBar;
- procedure CheckForErrors;
- public
- { Public declarations }
- end;
-
- const
- { define various useful constants }
- BUFFLEN = 255;
- BUTTONWIDTH = 100;
- FORMHEIGHT = 100; { height of form without a scrollbar visible }
- SCROLLFORMHEIGHT = 125; { height of form with a scrollbar visible }
- CONFIGFILE = 'Iconbar.sav'; { name of file to save/load buttonbar config }
-
- type
- CHARARRAY = array[0..BUFFLEN] of char;
- var
- Form1: TForm1;
- Icon : TIcon; {!! An icon to put onto a button }
-
- implementation
-
- uses errordlgunit;
- {$R *.DFM}
-
- function ExecuteFile(const FileName, Dir : string ) : THandle;
- {!! A simple interface to the API's ShellExecute function that opens
- the application specified by FileName }
- var
- ntFileName, ntDir : CHARARRAY;
- begin
- Result := ShellExecute(Application.MainForm.Handle,
- nil, { nil here equates to the default command,'Open' }
- StrPCopy(ntFileName, FileName),
- nil,
- StrPCopy(ntDir,Dir),
- SW_SHOW);
- end;
-
- procedure TForm1.DealWithScrollBar;
- {!! If there are more buttons than fit onto the form, add a scrollbar
- and expand the height of the form to make room for it.
- Note Scrollbar.Range must be greater than the Client width of the
- form for the Scrollbar to appear }
- var
- bw : integer;
- begin
- bw := ButtonList.Count * BUTTONWIDTH;
- if bw > Form1.ClientWidth then
- Form1.Height := SCROLLFORMHEIGHT
- else
- Form1.Height := FORMHEIGHT;
- HorzScrollBar.Range := bw;
- end;
-
- procedure TForm1.ClearAll;
- {!! Clear buttons from Form and from ButtonList }
- var
- i : integer;
- begin
- // Remove the buttons from the form
- for i := 0 to (ButtonList.Count-1) do
- TBitBtn(ButtonList.Objects[i]).Parent := nil;
- // Clear the ButtonList
- ButtonList.Clear;
- end;
-
- procedure TForm1.AddErrorMsg( msg : string );
- {!! Add error message, msg, to the Memo on the ErrorDlg form }
- begin
- ErrorDlg.ErrMemo.Lines.Add( msg );
- end;
-
- procedure TForm1.CheckForErrors;
- {!! If there is any text in the Memo on the ErrorDlg form
- these are error messages so show the ErrorDlg form }
- begin
- if ErrorDlg.ErrMemo.Text <> '' then
- ErrorDlg.ShowModal;
- ErrorDlg.ErrMemo.Clear;
- end;
-
-
- procedure TForm1.AddBtn( fname : string );
- { Create a new application launching button, complete with glyph
- and label. Add it to the Form }
- var
- lplicon : word; {!! required argument to ExtractAssociatedIcon }
- buffer : CHARARRAY; { PChar version of the filename fname }
- begin
- lplicon := 0;
- StrPCopy(buffer,fname);
- if not FileExists(fname) then
- AddErrorMsg('Load error: Can''t locate ' + fname )
- else
- begin { if FileExists }
- {!! get icon for EXE file or of application associated with file }
- Icon.Handle := ExtractAssociatedIcon(HInstance, buffer, lplicon);
- ButtonList.AddObject(fname, TBitBtn.Create(Self));
- with TBitBtn(ButtonList.Objects[ButtonList.Count-1]) do
- begin
- with Glyph do
- begin
- width := 32;
- height := 32;
- if Icon.Handle <> 0 then
- Canvas.Draw(0,0,Icon);
- end;
- { set the size, font etc. of the BitBtn itself }
- width := BUTTONWIDTH;
- layout := blGlyphTop;
- font.name := 'Arial';
- font.size := 8;
- caption := ExtractFileName(fname);
- Align := alLeft;
- Parent := Form1; { put the BitBtn onto the form }
- OnClick := BtnClick; { set BitBtn's OnClick event-handler }
- end; { with TBitBtn }
- end; { if FileExists }
- end;
-
- procedure TForm1.RespondToMessage(var Msg: Tmsg; var Handled: Boolean);
- { Iterate through all file names if a multi-file selection was dropped }
- const
- FileIndex : Cardinal = Cardinal(-1); { return a count of dropped files }
- var { $FFFF 16-bit; $FFFFFFFF 32-bit }
- buffer : CHARARRAY;
- fname : string;
- fnum : word;
- begin
- if Msg.Message = WM_DROPFILES then
- begin
- for fnum := 0 to DragQueryFile(Msg.WParam, FileIndex, NIL, BUFFLEN)-1 do
- begin
- DragQueryFile(Msg.WParam, fnum, buffer, BUFFLEN);
- fname := StrPas(buffer);
- AddBtn(fname);
- end;
- DragFinish(Msg.WParam);
- Handled := True;
- CheckForErrors;
- DealWithScrollBar;
- end;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- { make this form drag-friendly }
- DragAcceptFiles(Form1.Handle, true);
- Application.OnMessage := RespondToMessage;
- ButtonList := TStringList.Create; { Create StringList to store our BitBtns }
- end;
-
- procedure TForm1.BtnClick(Sender: TObject);
- { The BitBtns' OnClick event-handler }
- { The Sender parameter indicates which button on the form was clicked.
- We locate the position of the button in our ButtonList.
- ButtonList is a StringList containing a String (the executable file's path)
- alongside each button. We are able to use pass this string (and also to
- parse out the directory part of the string) to a function that launches the
- application.
- }
- var
- filename, errmsg : string;
- retval : word;
- begin
- errmsg := 'Couldn''t run ' + filename;
- filename := ButtonList.Strings[ButtonList.IndexOfObject(Sender)];
- retval := ExecuteFile(filename, ExtractFilePath(filename));
- if retval <= 32 then {!! i.e. if it's an error }
- begin
- case (retval) of {!! check retval against ShellAPI constants }
- ERROR_FILE_NOT_FOUND : errmsg := filename + ' not found!';
- ERROR_PATH_NOT_FOUND : errmsg := 'Path to ' + filename + 'not found!';
- SE_ERR_NOASSOC : errmsg := 'There is no application associated'
- + ' with ' + filename + '!';
- end;
- ShowMessage('ERROR!: ' + errmsg);
- end;
- end;
-
- procedure TForm1.ReloadButtons;
- {!! Reloads the Buttonbar from CONFIGFILE.
- NOTE: No check is made that CONFIGFILE exists.
- It is up to the calling procedure to cgeck this. }
- var
- templist : TStringList;
- i : integer;
- begin
- //{!! Remove the buttons
- ClearAll;
- //{!! Then rebuild the Panel and List from saved file names
- templist := TStringList.Create;
- templist.LoadFromFile(CONFIGFILE);
- for i := 0 to (templist.count-1) do
- AddBtn(templist.Strings[i]);
- templist.Free;
- CheckForErrors;
- DealWithScrollBar;
- end;
-
- procedure TForm1.SaveMenuItemClick(Sender: TObject);
- begin
- ButtonList.SaveToFile(CONFIGFILE);
- end;
-
-
- procedure TForm1.ReloadMenuItemClick(Sender: TObject);
- begin
- if not FileExists(CONFIGFILE) then
- ShowMessage('Can''t load buttons. Config file '
- + CONFIGFILE + ' not found!' )
- else
- ReloadButtons;
- end;
-
- procedure TForm1.ClearAllMenuItemClick(Sender: TObject);
- begin
- ClearAll;
- DealWithScrollBar;
- end;
-
- procedure TForm1.ExitMenuItemClick(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TForm1.FormShow(Sender: TObject);
- begin
- {!! Load Saved Buttons if a CONFIGFILE exists }
- if not FileExists(CONFIGFILE) then
- ShowMessage('Config file '
- + CONFIGFILE + ' not found! Loading an empty AppLauncher bar' )
- else
- ReloadButtons;
- end;
-
- end.
-